/*
 *  Class name: DatabaseDataCreatorBean.java
 *  Version: 1.0
 *  Date: 25.1.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.managedbeans.application;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.GoalDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.PlayerDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.SoccerMatchDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.TeamDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.services.SoccerMatchServiceBeanLocal;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.services.TeamServiceBeanLocal;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

/**
 *
 * @author Michal Ďuriš
 */
@Named(value = "databaseDataCreatorBean")
@ApplicationScoped
public class DatabaseDataCreatorBean {

    @EJB
    private TeamServiceBeanLocal teamService;

    @EJB
    private SoccerMatchServiceBeanLocal soccerMatchService;

    private List<TeamDTO> teams = new ArrayList<>();
    private final List<SoccerMatchDTO> soccerMatches = new ArrayList<>();

    /**
     * Creates a new instance of DatabaseDataCreatorBean
     */
    public DatabaseDataCreatorBean() {
    }

    @PostConstruct
    public void init() {

        createTeams();
    }

    public void storeData() {

        if (teamService.getFilteredTeamsCount(null) == 0) {

            for (TeamDTO team : teams) {
                team.setId(teamService.addTeam(team).getId());
            }

            teams = teamService.getAllTeams();

            createMatches();

            for (SoccerMatchDTO match : soccerMatches) {
                soccerMatchService.addSoccerMatch(match);
            }

        }
    }

    private void createTeams() {

        createArsenal();
        createFulham();
        createAstonVilla();
        createTottenham();
        createCardiff();
    }

    private void createMatches() {

        createARSvsFFCMatch();
        createAVLvsARSMatch();
        createARSvsTHSMatch();
        createARSvsCARMatch();

    }

    private void createArsenal() {
        TeamDTO arsenal = new TeamDTO();

        arsenal.setManagerName("Arsene Wenger");
        arsenal.setName("Arsenal FC");
        arsenal.setPlayers(createArsenalSquad(arsenal));

        teams.add(arsenal);
    }

    private static List<PlayerDTO> createArsenalSquad(TeamDTO arsenal) {

        List<PlayerDTO> squad = new ArrayList<>();

        squad.add(createPlayer("Wojciech", "Szczesny", 1, arsenal));
        squad.add(createPlayer("Bacary", "Sagna", 3, arsenal));
        squad.add(createPlayer("Per", "Mertesacker", 4, arsenal));
        squad.add(createPlayer("Thomas", "Vermaelen", 5, arsenal));
        squad.add(createPlayer("Laurent", "Koscielny", 6, arsenal));
        squad.add(createPlayer("Tomáš", "Rosický", 7, arsenal));
        squad.add(createPlayer("Mikel", "Arteta", 8, arsenal));
        squad.add(createPlayer("Lukas", "Podolski", 9, arsenal));
        squad.add(createPlayer("Jack", "Wilshere", 10, arsenal));

        squad.add(createPlayer("Mesut", "Özil", 11, arsenal));
        squad.add(createPlayer("Olivier", "Giroud", 12, arsenal));
        squad.add(createPlayer("Emiliano", "Viviano", 13, arsenal));
        squad.add(createPlayer("Theo", "Walcott", 14, arsenal));
        squad.add(createPlayer("Alex", "Oxlade-Chamberlain", 15, arsenal));
        squad.add(createPlayer("Aaron", "Ramsey", 16, arsenal));
        squad.add(createPlayer("Nacho", "Monreal", 17, arsenal));
        squad.add(createPlayer("Santiago", "Cazorla", 19, arsenal));
        squad.add(createPlayer("Mathieu", "Flamini", 20, arsenal));

        squad.add(createPlayer("Lukasz", "Fabianski", 21, arsenal));
        squad.add(createPlayer("Yaya", "Sanogo", 22, arsenal));
        squad.add(createPlayer("Nicklas", "Bendtner", 23, arsenal));
        squad.add(createPlayer("Vassiriki Abou", "Diaby", 24, arsenal));
        squad.add(createPlayer("Carl", "Jenkinson", 25, arsenal));
        squad.add(createPlayer("Emmanuel", "Frimpong", 26, arsenal));
        squad.add(createPlayer("Kieran", "Gibbs", 28, arsenal));
        squad.add(createPlayer("Park", "Chu-Young", 30, arsenal));

        squad.add(createPlayer("Ryo", "Miyaichi", 31, arsenal));
        squad.add(createPlayer("Thomas", "Eisfeld", 42, arsenal));
        squad.add(createPlayer("Serge", "Gnabry", 44, arsenal));
        squad.add(createPlayer("Isaac", "Hayden", 45, arsenal));
        squad.add(createPlayer("Kristoffer", "Olsson", 53, arsenal));
        squad.add(createPlayer("Gedion", "Zelalem", 58, arsenal));
        squad.add(createPlayer("Alex", "Iwobi", 60, arsenal));
        squad.add(createPlayer("Nico", "Yennaris", 64, arsenal));

        return squad;
    }

    private void createFulham() {
        TeamDTO fulham = new TeamDTO();

        fulham.setManagerName("Rene Meulensteen");
        fulham.setName("Fulham FC");
        fulham.setPlayers(createFulhamSquad(fulham));

        teams.add(fulham);
    }

    private static List<PlayerDTO> createFulhamSquad(TeamDTO fulham) {

        List<PlayerDTO> squad = new ArrayList<>();

        squad.add(createPlayer("Maarten", "Stekelenburg", 1, fulham));
        squad.add(createPlayer("Steve", "Sidwell", 7, fulham));
        squad.add(createPlayer("Scott", "Parker", 28, fulham));
        squad.add(createPlayer("Dimitar", "Berbatov", 9, fulham));

        return squad;
    }

    private void createAstonVilla() {
        TeamDTO astonVilla = new TeamDTO();

        astonVilla.setManagerName("Paul Lambert");
        astonVilla.setName("Aston Villa FC");
        astonVilla.setPlayers(createAstonVillaSquad(astonVilla));

        teams.add(astonVilla);
    }

    private static List<PlayerDTO> createAstonVillaSquad(TeamDTO astonVilla) {

        List<PlayerDTO> squad = new ArrayList<>();

        squad.add(createPlayer("Brad", "Guzan", 1, astonVilla));
        squad.add(createPlayer("Karim", "El Ahmadi", 8, astonVilla));
        squad.add(createPlayer("Christian", "Benteke", 20, astonVilla));
        squad.add(createPlayer("Gabriel", "Agbonlahor", 11, astonVilla));

        return squad;
    }

    private void createTottenham() {
        TeamDTO tottenham = new TeamDTO();

        tottenham.setManagerName("Tim Sherwood");
        tottenham.setName("Tottenham Hotspur FC");
        tottenham.setPlayers(createTottenhamSquad(tottenham));

        teams.add(tottenham);
    }

    private static List<PlayerDTO> createTottenhamSquad(TeamDTO tottenham) {

        List<PlayerDTO> squad = new ArrayList<>();

        squad.add(createPlayer("Hugo", "Lloris", 25, tottenham));
        squad.add(createPlayer("Aaron", "Lennon", 7, tottenham));
        squad.add(createPlayer("Mousa", "Dembélé", 19, tottenham));
        squad.add(createPlayer("Emmanuel", "Adebayor", 10, tottenham));

        return squad;
    }

    private void createCardiff() {
        TeamDTO cardiff = new TeamDTO();

        cardiff.setManagerName("Ole Gunnar Solskjaer");
        cardiff.setName("Cardiff City FC");
        cardiff.setPlayers(createCardiffSquad(cardiff));

        teams.add(cardiff);
    }

    private static List<PlayerDTO> createCardiffSquad(TeamDTO cardiff) {

        List<PlayerDTO> squad = new ArrayList<>();

        squad.add(createPlayer("David", "Marshall", 1, cardiff));
        squad.add(createPlayer("Kevin", "McNaughton", 25, cardiff));
        squad.add(createPlayer("Kim", "Bo-Kyung", 13, cardiff));
        squad.add(createPlayer("Fraizer", "Campbell", 10, cardiff));

        return squad;
    }

    private static PlayerDTO createPlayer(String firstName, String lastName,
                                          int shirtNumber, TeamDTO team) {
        PlayerDTO player = new PlayerDTO();

        player.setFirstName(firstName);
        player.setLastName(lastName);
        player.setShirtNumber(shirtNumber);
        player.setTeam(team);

        return player;
    }

    private void createARSvsFFCMatch() {
        SoccerMatchDTO match = new SoccerMatchDTO();

        match.setHomeTeam(teams.get(0));
        match.setAwayTeam(teams.get(1));
        match.setStadiumName("Emirates Stadium");
        match.setMatchDate(createUKDate(2014, 0, 18, 15, 00));

        List<GoalDTO> goals = new ArrayList<>();

        goals.add(createGoal(teams.get(0).getPlayers().get(16), 58, match));
        goals.add(createGoal(teams.get(0).getPlayers().get(16), 62, match));

        match.setGoals(goals);

        soccerMatches.add(match);
    }

    private void createAVLvsARSMatch() {

        SoccerMatchDTO match = new SoccerMatchDTO();

        match.setHomeTeam(teams.get(2));
        match.setAwayTeam(teams.get(0));
        match.setStadiumName("Villa Park");
        match.setMatchDate(createUKDate(2014, 0, 13, 20, 00));

        List<GoalDTO> goals = new ArrayList<>();

        goals.add(createGoal(teams.get(2).getPlayers().get(2), 76, match));

        goals.add(createGoal(teams.get(0).getPlayers().get(8), 34, match));
        goals.add(createGoal(teams.get(0).getPlayers().get(10), 35, match));

        match.setGoals(goals);

        soccerMatches.add(match);
    }

    private void createARSvsTHSMatch() {
        SoccerMatchDTO match = new SoccerMatchDTO();

        match.setHomeTeam(teams.get(0));
        match.setAwayTeam(teams.get(3));
        match.setStadiumName("Emirates Stadium");
        match.setMatchDate(createUKDate(2014, 0, 4, 17, 15));

        List<GoalDTO> goals = new ArrayList<>();

        goals.add(createGoal(teams.get(0).getPlayers().get(16), 32, match));
        goals.add(createGoal(teams.get(0).getPlayers().get(5), 62, match));

        match.setGoals(goals);

        soccerMatches.add(match);
    }

    private void createARSvsCARMatch() {
        SoccerMatchDTO match = new SoccerMatchDTO();

        match.setHomeTeam(teams.get(0));
        match.setAwayTeam(teams.get(4));
        match.setStadiumName("Emirates Stadium");
        match.setMatchDate(createUKDate(2014, 0, 1, 15, 00));

        List<GoalDTO> goals = new ArrayList<>();

        goals.add(createGoal(teams.get(0).getPlayers().get(20), 88, match));
        goals.add(createGoal(teams.get(0).getPlayers().get(12), 92, match));

        match.setGoals(goals);

        soccerMatches.add(match);
    }

    private static Date createUKDate(int year, int month, int day, int hour,
                                     int minute) {

        GregorianCalendar calendar = new GregorianCalendar(Locale.UK);

        calendar.setLenient(false);

        calendar.set(year, month, day, hour, minute);

        return new Date(calendar.getTimeInMillis());
    }

    private static GoalDTO createGoal(PlayerDTO scorer,
                                      int scoreMinute,
                                      SoccerMatchDTO match) {

        GoalDTO goal = new GoalDTO();

        goal.setPlayer(scorer);
        goal.setScoreTime(scoreMinute);
        goal.setSoccerMatch(match);

        return goal;
    }

}
